package day15;

public class SimpleFactory {
    //根据传入的参数  创建不同类型的产品对象
    public static Product createObject(String type){
        Product p = null;
        switch(type){
            case "A":p = new ProductA();break;
            case "B":p = new ProductB();break;
            case "C":p = new ProductC();break;
        }
        return p;
    }
    public static void main(String[] args) {
        Product p1 = SimpleFactory.createObject("A");
        Product p2 = SimpleFactory.createObject("B");
        Product p3 = SimpleFactory.createObject("C");
        p1.product();
        p2.product();
        p3.product();
    }
}

//定义一些接口

interface Product{ void product();}
//具体产品A
class ProductA implements Product{
    public void product(){
        System.out.println("生产A");
    }
}

class ProductB implements Product{
    public void product(){
        System.out.println("生产B");
    }
}

class ProductC implements Product{
    public void product(){
        System.out.println("生产C");
    }
}

//测试工厂方法模式
interface MethodFactory{
    Product createObject();
}

//具体的工具工厂实现类
class FactoryA implements MethodFactory{
    public Product createObject(){
        return new ProductA();
    }
}

class FactoryB implements MethodFactory{
    public Product createObject(){
        return new ProductB();
    }
}

class FactoryC implements MethodFactory{
    public Product createObject(){
        return new ProductC();
    }
    public static void main(String[] args){
        MethodFactory factoryA = new FactoryA();
        Product pA = factoryA.createObject();
        MethodFactory factoryB = new FactoryB();
        Product pB = factoryB.createObject();
        MethodFactory factoryC = new FactoryC();
        Product pC = factoryC.createObject();
        pA.product();
        pB.product();
        pC.product();
    }
}